package handlers

import (
	"encoding/json"
	"generalhandler/conf"
	"generalhandler/utils"
)

type ScanlineEpHandler struct {

}

func (seh *ScanlineEpHandler) Handle(bytesMsg []byte, log Logger, pub Publisher) error {
	var mapMsg map[string]interface{}
	err := json.Unmarshal(bytesMsg, &mapMsg)
	if nil != err {
		log.Warn("failed to unmarshal %s, cuz %s", string(bytesMsg), err.Error())
		return nil
	}

	ifDomain, _ := mapMsg["domain"]
	strDomain, _ := ifDomain.(string)
	if 0 == len(strDomain) {
		log.Warn("found no domain in %s", string(bytesMsg))
		return nil
	}

	ifTkId, _ := mapMsg["tkid"]
	strTkId, _ := ifTkId.(string)
	if 0 == len(strTkId) {
		log.Warn("found no tkid in %s", string(bytesMsg))
		return nil
	}

	var ignoreMap map[string]interface{}
	ignoreMap = make(map[string]interface{})
	if ifIgnore, ok := mapMsg["ignore"]; ok{
		strIgnore, _ := ifIgnore.(string)
		utils.StringToMap(strIgnore,ignoreMap)
	}
	for _, e := range conf.MQConfigInstance().NextHandlers {
		if _, ok := ignoreMap[e]; ok{
			continue
		}
		pub.PublishMessage(e, bytesMsg)
	}

	return nil
}

